package Dao;

import bean.User;


import java.sql.Connection;
import java.util.List;

public class UserDaoImpl extends BaseDao implements UserDao {
    public UserDaoImpl() {
    }

    @Override
    /**
     * 通过传入user对象方法，来修改表中的数据
     * @user : 传入要修改的信息user_tab
     * return : 修改成功返回 > 0的数，否则 < 0;
     */
    public int update(User user) {
        String sql = "update user_tab set name = ? ,balance = ? where id = ?";
        int updateCount = testQueryRunnerUpdate(sql, user);
        return updateCount;
    }

    @Override
    /**
     * 通过传入user对象方法，来删除表中的数据
     * @user : 传入要删除的信息user_tab
     * return : 删除成功返回 > 0的数，否则 < 0;
     */
    public int delete(int id) {
        String sql = "delete from user_tab where id = ?";
        int deleteCount = testQueryRunnerDelete(sql, id);
        return deleteCount;
    }

    @Override
    public int insertInfo(User user) {
        String sql = "insert into user_tab(id,name,balance) values(?,?,?)";
        int insertCount = testQueryRunnerUpdate(sql, user);
        return insertCount;
    }

    @Override
    public User selectOneInfo(int id) {
        String sql = "select id,name,balance from user_tab where id = ?";
        User user = testQueryRunnerSelectOneInfo(id, sql);
        return user;
    }

    @Override
    public List<User> selectManInfo(String keyWord,int pageNum) {
        String sql = "select id,name,balance from user_tab where name like ? limit ? ,5";
        List<User> list = testQueryRunnerSelectManyInfo(sql,"%"+keyWord+"%",pageNum);
        return list;
    }

    @Override
    public Long selectNums(String keyWord) {
        String sql = "select count(*) from user_tab where name like ?";
        Object info = testQueryRunnerSelectSpeciInfo(sql,"%"+keyWord+"%");
        Long count = (Long) info;
        return count;
    }

    @Override
    public Double selectHighestBalance() {
        String sql = "select MAX(balance) from user_tab ";
        Object o = testQueryRunnerSelectSpeciInfo(sql);
        Double balance = (Double) o;
        return balance;
    }

    //模糊查询

}
